﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DomainSearchTools.Common
{
    /// <summary>
    /// Whois服务器
    /// </summary>
    public class WhoisServer
    {
        public static string GetServer(string suffix)
        {
            //return "whois.www.net.cn";
            string result = "";
            switch (suffix)
            {

                case ".gs":
                case ".ms":
                case ".tc":
                case ".tf":
                case ".vg":
                    result = "whois.adamsnames.tc";
                    break;
                case ".info":
                    result = "whois.afilias.info"; //whois.afilias.net不通
                    break;

                case ".am":
                    result = "whois.amnic.net";
                    break;
                case ".hk":
                    result = "whois.apnic.net";//亚洲与太平洋地区网络信息中心 供查内容 东亚(包括中国大陆和台湾)、南亚、大洋洲IP地址注册信息
                    break;
                case ".au":
                    result = "whois.aunic.net";
                    break;
                case ".com.au":
                case ".net.au":
                case ".org.au":
                case ".asn.au":
                case ".id.au":
                    result = "whois.ausregistry.net.au";
                    break;
                case ".br.com":
                case ".cn.com":
                case ".de.com":
                case ".eu.com":
                case ".gb.com":
                case ".gb.net":
                case ".hu.com":
                case ".no.com":
                case ".qc.com":
                case ".ru.com":
                case ".sa.com":
                case ".se.com":
                case ".se.net":
                case ".uk.com":
                case ".uk.net":
                case ".us.com":
                case ".uy.com":
                case ".za.com":
                    result = "whois.centralnic.com";
                    break;
                case ".ca":
                    result = "whois.cira.ca";
                    break;
                case ".com.cn":
                case ".org.cn":
                case ".net.cn":
                case ".cn":
                    result = "whois.cnnic.net.cn";//中国互联网络信息中心  .cn域名(除.edu.cn)信息 whois.cnnic.cn
                    break;
                case ".asia":
                case ".com":
                case ".net":
                    result = "whois.crsnic.net";
                    break;
                case ".de":
                    result = "whois.denic.de";
                    break;
                case ".edu.cn":
                    result = "whois.edu.cn";//中国教育与科研计算机网网络信息中心 供查内容 中国教育网内的IP地址和.edu.cn域名信息
                    break;
                case ".dk":
                    result = "whois.dk-hostmaster.dk";
                    break;
                case ".be":
                    result = "whois.dns.be";
                    break;
                case ".lu":
                    result = "whois.dns.lu";
                    break;
                case ".pl":
                    result = "whois.dns.pl";
                    break;
                case ".ie":
                    result = "whois.domainregistry.ie";
                    break;
                case ".nl":
                    result = "whois.domain-registry.nl";
                    break;
                case ".edu":
                    result = "whois.educause.net";
                    break;
                case ".ee":
                    result = "whois.eenet.ee";
                    break;
                case ".eu":
                    result = "whois.eu";
                    break;
                case ".int":
                    result = "whois.iana.org";
                    break;
                case ".aero":
                    result = "whois.information.aero";
                    break;
                case ".il":
                    result = "whois.isoc.org.il";
                    break;
                case ".ac.uk":
                    result = "whois.ja.net";
                    break;
                case ".gov.uk":
                    result = "whois.ja.net";
                    break;
                case ".jp":
                    result = "whois.jp";//whois.nic.ad.jp  日本互联网络信息中心(日本东京) 供查内容 .jp域名和日本境内的IP地址信息 
                    break;
                case ".kr":
                    result = "whois.krnic.net";//韩国互联网络信息中心(韩国汉城) 供查内容 .kr域名和韩国境内的IP地址信息
                    break;
                case ".museum":
                    result = "whois.museum";
                    break;
                case ".in":
                    result = "whois.ncst.ernet.in";
                    break;
                case ".biz":
                    result = "whois.neulevel.biz";
                    break;
                case ".ac":
                    result = "whois.nic.ac";
                    break;
                case ".as":
                    result = "whois.nic.as";
                    break;
                case ".at":
                    result = "whois.nic.at";
                    break;
                case ".br":
                    result = "whois.nic.br";
                    break;
                case ".cc":
                    result = "whois.nic.cc";
                    break;
                case ".cd":
                    result = "whois.nic.cd";
                    break;
                case ".ch":
                    result = "whois.nic.ch";
                    break;
                case ".li":
                    result = "whois.nic.ch";
                    break;
                case ".cl":
                    result = "whois.nic.cl";
                    break;
                case ".coop":
                    result = "whois.nic.coop";
                    break;
                case ".cx":
                    result = "whois.nic.cx";
                    break;
                case ".fr":
                    result = "whois.nic.fr";
                    break;
                case ".gov":
                    result = "whois.nic.gov";
                    break;
                case ".ir":
                    result = "whois.nic.ir";
                    break;
                case ".it":
                    result = "whois.nic.it";
                    break;
                case ".mil":
                    result = "whois.nic.mil";
                    break;
                case ".mx":
                    result = "whois.nic.mx";
                    break;
                case ".name":
                    result = "whois.nic.name";
                    break;
                case ".kh":
                    result = "whois.nic.net.kh";
                    break;
                case ".sg":
                    result = "whois.nic.net.sg";
                    break;
                case ".nu":
                    result = "whois.nic.nu";
                    break;
                case ".sh":
                    result = "whois.nic.sh";
                    break;
                case ".tj":
                    result = "whois.nic.tj";
                    break;
                case ".uk":
                    result = "whois.nic.uk";
                    break;
                case ".us":
                    result = "whois.nic.us";
                    break;
                case ".ws":
                    result = "whois.nic.ws";
                    break;
                case ".se":
                    result = "whois.nic-se.se";
                    break;
                case ".no":
                    result = "whois.norid.no";
                    break;
                case ".org":
                    result = "whois.publicinterestregistry.net";
                    break;
                case ".al":
                case ".az":
                case ".ba":
                case ".bg":
                case ".by":
                case ".cy":
                case ".cz":
                case ".dz":
                case ".eg":
                case ".es":
                case ".fi":
                case ".fo":
                case ".gb":
                case ".ge":
                case ".gr":
                case ".hr":
                case ".hu":
                case ".is":
                case ".lt":
                case ".lv":
                case ".ma":
                case ".md":
                case ".mk":
                case ".mt":
                case ".pt":
                case ".ro":
                case ".si":
                case ".sk":
                case ".sm":
                case ".tn":
                case ".tr":
                case ".ua":
                case ".va":
                    result = "whois.ripe.net";// 欧州IP地址注册中心(荷兰阿姆斯特丹) 供查内容 欧洲、北非、西亚地区的IP地址信息 
                    break;

                case ".su":
                    result = "whois.ripn.net";
                    break;
                case ".ru":
                    result = "whois.ripn.ru";
                    break;
                case ".nz":
                    result = "whois.srs.net.nz";
                    break;
                case ".th":
                    result = "whois.thnic.net";
                    break;
                case ".to":
                    result = "whois.tonic.to";
                    break;
                case ".tv": //whois.tv目前不通
                    result = "whois.nic.tv";
                    break;
                case ".tw":
                    result = "whois.twnic.net";//台湾互联网络信息中心 供查内容 .tw域名和部分台湾岛内IP地址信息
                    break;
                default:
                    result = "whois.internic.net";//互联网络信息中心(美国洛杉矶市Marina del Rey镇)
                    break;
            }
            return result;
        }
    }


    /// <summary>
    /// Whois命令
    /// </summary>
    public class WhoisCommand
    {
        /// <summary>
        /// 查询一个域名的信息 domain name, registrar name,whois server and URL, Nameserver name and IP Addresses, and updated date
        /// 如 domain netsol.com
        /// </summary>
        public static string domain = "domain";
        /// <summary>
        /// 查询域名信息（会有多个） nameserver name, registrar name,IP addresses, Whois Server name and URL
        /// 如 nameserver baidu.com 或 nameserver 101.198.1.101
        /// </summary>
        public static string nameserver = "nameserver";
        /// <summary>
        /// 查询注册信息 Registrar name, mail, address, phone number and contact information
        /// 如registrar Network Solutions, Inc.
        /// </summary>
        public static string registrar = "registrar";
    }


    /// <summary>
    /// 获取whois数据
    /// </summary>
    public class WhoisInfo
    {
        public static string Key = "Domain Name,Registrar,Sponsoring Registrar,Whois Server,Name Server,Status,Updated Date,Creation Date,Registration Date,Expiration Date,Registry Expiry Date,Company";

        /// <summary>
        /// 解析whois详情
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Parse(string content)
        {
            Dictionary<string, string> list = new Dictionary<string, string>();
            string[] Keys = Key.Split(',');
            string[] rows = content.Split('\n');
            //获取所有的数据
            foreach (var row in rows)
            {
                for (var i = 0; i < Keys.Length; i++)
                {
                    if (row.Contains(Keys[i] + ":"))
                    {
                        if (list.ContainsKey(Keys[i]))
                            list[Keys[i]] += "," +  row.Replace(Keys[i] + ":", "").Trim();
                        else 
                            list.Add(Keys[i], row.Replace(Keys[i] + ":", "").Trim());
                        break;
                    }
                }
            }

            ////重叠key值一样的数据
            //Dictionary<string, string> list = new Dictionary<string, string>();
            //foreach (var d in list_all)
            //{
            //    if (list.ContainsKey(d.Key))
            //        list[d.Key] += "," + d.Value;
            //    else
            //        list.Add(d.Key, d.Value);
            //}

            var result = "";
            foreach (var d in list)
            {
                result += string.Format(@"{0}:{1}

", d.Key, d.Value);
            }
            return result;
        }
    }

}
